package com.dingdang.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dingdang.data.Result;
import com.dingdang.dto.CarGoodsDTO;
import com.dingdang.dto.UserDTO;
import com.dingdang.entity.Car;
import com.dingdang.feign.GoodsFeign;
import com.dingdang.service.CollectServiceI;
import com.dingdang.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class CollectServiceIImpl implements CollectServiceI {
    @Resource
    private MongoTemplate mongoTemplate;
    @Resource
    private JwtUtil jwtUtil;
    @Resource
    private GoodsFeign goodsFeign;
//    查询
    @Override
    public List<Car> findCollectCar(Long uid) {
        Query  query = new Query();
        query.addCriteria(Criteria.where("uid").is(uid));
       return mongoTemplate.find(query, Car.class);
    }

    @Override
    public void addCollectCar(HttpServletRequest request, Long carId) {
        Car car = new Car();
        String token = request.getHeader("authToken");
        Claims jwt = jwtUtil.parseJwt(token);
        UserDTO dto = JSONObject.parseObject(jwt.getSubject(), UserDTO.class);
        BeanUtils.copyProperties(dto,car);
        Result<CarGoodsDTO> carGoodsDTO =goodsFeign.ToUpdateAdmin(carId);
        BeanUtils.copyProperties(carGoodsDTO.getData(),car);
        car.setCollectTime(new Date());
        mongoTemplate.insert(car);

    }
}
